var gulp = require('gulp')
var ts = require('gulp-typescript')
var tsProject = ts.createProject('tsconfig.json')
var tsShareProject = ts.createProject('tsconfig.json')
var stylus = require('gulp-stylus')
var sourcemap = require('gulp-sourcemaps')

gulp.task('typescript', function () {
  return gulp.src('./src/**')
    .pipe(sourcemap.init())
    .pipe(tsProject())
    .js
    .pipe(sourcemap.write('./maps/'))
    .pipe(gulp.dest('dist'))
})
gulp.task('typescript:share', function () {
  return gulp.src('./share/*.ts')
    .pipe(sourcemap.init())
    .pipe(tsShareProject())
    .js
    .pipe(sourcemap.write())
    .pipe(gulp.dest('./share'))
})
gulp.task('typescript:pages', function () {
  return gulp.src('./pages/*.ts')
    .pipe(sourcemap.init())
    .pipe(tsProject())
    .js
    .pipe(sourcemap.write())
    .pipe(gulp.dest('./pages'))
})

gulp.task('stylus', function () {
  return gulp.src('./pages/*.styl')
    .pipe(sourcemap.init())
    .pipe(stylus())
    .pipe(sourcemap.write())
    .pipe(gulp.dest('./pages'))
})

gulp.task('watch', function () {
  gulp.watch('src/**', ['typescript'])
  gulp.watch(['share/*.ts'], ['typescript:share'])
  gulp.watch(['pages/**/*.styl'], ['stylus'])
  gulp.watch(['pages/**/*.ts'], ['typescript:pages'])
})

gulp.task('default', ['typescript', 'typescript:share', 'stylus', 'watch'])
